# 对应CSDN python系列专栏博客
# 写于2024 04 16
# 主题：穿越Python的迭代之旅：while，for 循环的奇妙世界

# 1.1 while 语句

n = 100
sum = 0
i = 1
while i<=n:
    sum = sum +i
    i += 1
print(f"1到{n}之和为:{sum}")

mul = 1
i = 1
sum = 0
while i<= 10:
    mul = mul * i
    sum = sum +mul
    i = i+1
else:
    print("循环结束！")
print(f"sum = {sum}")

total_score = 0
average_score = 0.0
count = 0
English_score = int(input("请输入学生的英语成绩:"))
while English_score != -1:
    total_score += English_score
    count += 1
    English_score = int(input("请输入学生的英语成绩:"))
average_score = total_score / count
print(f"录入英语成绩{count}份,学生总成绩{total_score},平均成绩{average_score}")

# 1.2 for语句

my_list = ['C', 'I', 'L', 'M', 'Y', 2, 3]

for i in my_list:
    print(i, end=" ")

my_tuple = ('C', 'I', 'L', 'M', 'Y', 2, 3)

for i in my_tuple:
    print(i, end=" ")
else:
    print("遍历结束")

for i in range(1,100):
    if i % 3 == 0:
        print(f"{i}",end=" ")
# 2.1 break
n1 = 1
n2 = 1
n3 = 2
count = 3
print("斐波那契前十项:", n1, n2, n3, end=" ")
while True:
    if count >= 10:
        break
    else:
        n1 = n2
        n2 = n3
        n3 = n1 + n2
        count += 1
        print(n3, end=" ")

# 2.2 continue
i = 1
sum = 0
for i in range(1, 100):
    if i % 2 == 1:
        continue
    sum += i
print(f"1-100的所有偶数和: {sum}")

# pass
i = 1
sum = 0
for i in range(1, 100):
    if i % 2 == 0:
        sum += i
else:
    pass
print(f"1-100的所有偶数和: {sum}")

# 循环中的循环
for i in range(5):
    for j in range(i + 1):
        print("*", end=" ")
    print()

for i in range(4):  # 控制行数
    for j in range(4):  # 控制列数
        if i == j:
            print("1", end=" ")
        else:
            print("0", end=" ")
    print()  # 换行

